package com.ruoyi.life.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.domain.LifeApartmentPerson;
import com.ruoyi.life.service.ILifeApartmentPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 公寓人员Controller
 *
 * @author cjie
 * @date 2022-11-26
 */
@Api(tags = " 公寓人员")
@RestController
@RequestMapping("/lifeApartmentPerson")
public class LifeApartmentPersonController extends BaseController {
    @Autowired
    private ILifeApartmentPersonService lifeApartmentPersonService;

    /**
     * 查询公寓人员列表
     */
    @ApiOperation(value="查询公寓人员列表", notes="查询公寓人员列表")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:list")
    @GetMapping("/list")
    public TableDataInfo list(LifeApartmentPerson lifeApartmentPerson) {
        startPage();
        List<LifeApartmentPerson> list = lifeApartmentPersonService.selectLifeApartmentPersonList(lifeApartmentPerson);
        return getDataTable(list);
    }

    /**
     * 导出公寓人员列表
     */
    @ApiOperation(value="导出公寓人员列表", notes="导出公寓人员列表")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:export")
    @Log(title = "公寓人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeApartmentPerson lifeApartmentPerson) throws IOException {
        List<LifeApartmentPerson> list = lifeApartmentPersonService.selectLifeApartmentPersonList(lifeApartmentPerson);
        ExcelUtil<LifeApartmentPerson> util = new ExcelUtil<LifeApartmentPerson>(LifeApartmentPerson.class);
        util.exportExcel(response, list, "公寓人员数据");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<LifeApartmentPerson> util = new ExcelUtil<LifeApartmentPerson>(LifeApartmentPerson.class);
        util.importTemplateExcel(response, "公寓人员数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<LifeApartmentPerson> util = new ExcelUtil<LifeApartmentPerson>(LifeApartmentPerson.class);
        List<LifeApartmentPerson> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = lifeApartmentPersonService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);
    }

    /**
     * 获取公寓人员详细信息
     */
    @ApiOperation(value="获取公寓人员详细信息", notes="获取公寓人员详细信息")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lifeApartmentPersonService.selectLifeApartmentPersonById(id));
    }

    /**
     * 获取公寓人员详细信息
     */
    @ApiOperation(value="获取公寓人员详细信息", notes="获取公寓人员详细信息")
    @GetMapping(value = "/getApartment/{parkNum}")
    public AjaxResult getApartment(@PathVariable("parkNum") String parkNum) {
        return AjaxResult.success(lifeApartmentPersonService.selectLifeApartmentByParkNum(parkNum));
    }

    /**
     * 新增公寓人员
     */
    @ApiOperation(value="新增公寓人员", notes="新增公寓人员")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:add")
    @Log(title = "公寓人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeApartmentPerson lifeApartmentPerson) {
        return toAjax(lifeApartmentPersonService.insertLifeApartmentPerson(lifeApartmentPerson));
    }

    /**
     * 修改公寓人员
     */
    @ApiOperation(value="修改公寓人员", notes="修改公寓人员")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:edit")
    @Log(title = "公寓人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeApartmentPerson lifeApartmentPerson) {
        return toAjax(lifeApartmentPersonService.updateLifeApartmentPerson(lifeApartmentPerson));
    }

    /**
     * 删除公寓人员
     */
    @ApiOperation(value="删除公寓人员", notes="删除公寓人员")
    @ApiImplicitParam(name = "ids", value = "公寓人员ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:lifeApartmentPerson:remove")
    @Log(title = "公寓人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lifeApartmentPersonService.deleteLifeApartmentPersonByIds(ids));
    }
}
